package com.xjrsoft.module.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.enums.MessageType;
import com.xjrsoft.common.enums.SystemConfigEnum;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.system.dto.*;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.entity.MessageTemplate;
import com.xjrsoft.module.system.service.IDictionarydetailService;
import com.xjrsoft.module.system.service.IMessageTemplateService;
import com.xjrsoft.module.system.vo.MessageTemplatePageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2024-01-16
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX+"/messageTemplate")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/messageTemplate", tags = "消息模板")
@AllArgsConstructor
public class MessageTemplateController {

    private final IMessageTemplateService messageTemplateService;

    private final IDictionarydetailService dictionarydetailService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "消息模板分页")
    public R page(MessageTemplatePageDto dto) {

        LambdaQueryWrapper<MessageTemplate> queryWrapper = Wrappers.lambdaQuery(MessageTemplate.class)
                .eq(StrUtil.isNotBlank(dto.getKeyword()),MessageTemplate::getName,dto.getKeyword())
                .eq(ObjectUtil.isNotNull(dto.getCategory()), MessageTemplate::getTemplateType, dto.getCategory())
                .like(StrUtil.isNotBlank(dto.getName()), MessageTemplate::getName, dto.getName())
                .like(StrUtil.isNotBlank(dto.getCode()), MessageTemplate::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getEnabledMark()), MessageTemplate::getEnabledMark, dto.getEnabledMark())
//                .orderByDesc(MessageTemplate::getSortCode)
                .orderByDesc(MessageTemplate::getCreateDate)
                .select(MessageTemplate.class, x -> VoToColumnUtil.fieldsToColumns(MessageTemplatePageVo.class).contains(x.getProperty()));
        IPage<MessageTemplate> page = messageTemplateService.page(ConventPage.getPage(dto), queryWrapper);

        PageOutput<MessageTemplatePageVo> pageOutput = ConventPage.getPageOutput(page, MessageTemplatePageVo.class);
        if (pageOutput.getList().size() > 0){
            List<DictionaryDetail> list = dictionarydetailService.list(Wrappers.<DictionaryDetail>lambdaQuery().eq(DictionaryDetail::getItemId, 1747131363295117314L));
            for (MessageTemplatePageVo messageTemplatePageVo : pageOutput.getList()) {
                Optional<DictionaryDetail> dictionaryDetailOptional = list.stream().filter(x -> x.getId().equals(messageTemplatePageVo.getTemplateType())).findFirst();
                if (dictionaryDetailOptional.isPresent()){
                    messageTemplatePageVo.setTemplateTypeName(dictionaryDetailOptional.get().getName());
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.EMAIL.getCode()){
                    messageTemplatePageVo.setMessageTypeName("邮件");
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.MESSAGE.getCode()){
                    messageTemplatePageVo.setMessageTypeName("短信");
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.WECHAT.getCode()){
                    messageTemplatePageVo.setMessageTypeName("企业微信");
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.DINGTALK.getCode()){
                    messageTemplatePageVo.setMessageTypeName("钉钉");
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.WEBHOOK.getCode()){
                    messageTemplatePageVo.setMessageTypeName("webhook");
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.WECHAT_OFFICIAL.getCode()){
                    messageTemplatePageVo.setMessageTypeName("微信公众号");
                }
                if (messageTemplatePageVo.getMessageType() == SystemConfigEnum.SYSTEM_MESSAGE.getCode()){
                    messageTemplatePageVo.setMessageTypeName("系统消息");
                }
            }
        }
        return R.ok(pageOutput);
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取详情")
    public R info(@RequestParam Long id){
        MessageTemplate messageTemplate = messageTemplateService.getById(id);
        return R.ok(messageTemplate);
    }


    @PostMapping
    @ApiOperation(value = "新增")
    public R add(@Valid @RequestBody AddMessageTemplateDto dto) {
        MessageTemplate messageTemplate = BeanUtil.toBean(dto, MessageTemplate.class);
        return R.ok(messageTemplateService.save(messageTemplate));
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public R update(@Valid @RequestBody UpdateMessageTemplateDto dto) {
        MessageTemplate messageTemplate = BeanUtil.toBean(dto, MessageTemplate.class);
        if (dto.getEnabledMark()){
            messageTemplate.setEnabledMark(EnabledMark.ENABLED.getCode());
        }else{
            messageTemplate.setEnabledMark(EnabledMark.DISABLED.getCode());
        }
        return R.ok(messageTemplateService.updateById(messageTemplate));
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(messageTemplateService.removeByIds(ids));
    }


    @PutMapping("/enabled")
    @ApiOperation(value = "启用/禁用")
    public R enabled(@Valid @RequestBody StampEnabledDto dto) {
        if (ObjectUtil.isNotEmpty(dto.getId())){
            MessageTemplate messageTemplate = messageTemplateService.getById(dto.getId());
            if (messageTemplate == null){
                throw new MyException("消息模板不存在");
            }else {
                if (messageTemplate.getEnabledMark() == 1){
                    messageTemplate.setEnabledMark(EnabledMark.DISABLED.getCode());
                }else {
                    messageTemplate.setEnabledMark(EnabledMark.ENABLED.getCode());
                }
            }
        }
        return R.ok();
    }

}
